package org.bai.algorithm.day01;

import java.util.Stack;

/**
 * @ClassName MinStack
 * @Description <a herf="https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/">包含min函数的栈</a>
 * @Author bai
 * @Date 2021/8/9 23:37
 * @Version 1.0
 **/
public class MinStack {

    private Stack<Integer> mainStack;
    private Stack<Integer> auxiliaryStack;

    /** initialize your data structure here. */
    public MinStack() {
        mainStack = new Stack<>();
        auxiliaryStack = new Stack<>();
    }

    public void push(int x) {
        if (auxiliaryStack.isEmpty() || auxiliaryStack.peek() > x) {
            auxiliaryStack.push(x);
        } else {
            auxiliaryStack.push(auxiliaryStack.peek());
        }
        mainStack.push(x);
    }

    public void pop() {
        mainStack.pop();
        auxiliaryStack.pop();
    }

    public int top() {
        return mainStack.peek();
    }

    public int min() {
        return auxiliaryStack.peek();
    }
}
